﻿using NovoSerDes.A2B.propeties_page;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Forms;

namespace NovoSerDes.A2B.Forms
{
    public partial class Master_propetites : UIForm
    {
        public Master_propetites()
        {
            InitializeComponent();
            // 设置 TabControl 的绘制模式和选项卡大小模式
            uiTabControl1.SizeMode = TabSizeMode.Fixed;
            uiTabControl1.DrawMode = TabDrawMode.OwnerDrawFixed; // 设置为自定义绘制模式
            uiTabControl1.DrawItem += new DrawItemEventHandler(tabControl1_DrawItem);


            // 添加 TabPages
            AddCustomTabPage(new General_View_Page(), "General View");

            AddCustomTabPage(new Register_View(), "Register View");


            AdjustTabSizes();

        }
        private void AddCustomTabPage(UIPage tabPage, string text)
        {
            tabPage.Text = text; // 设置 TabPage 的文本
            uiTabControl1.AddPage(tabPage); // 添加 TabPage 到 TabControl
        }
        /// <summary>
        /// 遍历所有选项卡，计算最大文本宽度。设置 uiTabControl1.ItemSize，确保每个选项卡有足够的宽度容纳文本。
        /// </summary>
        private void AdjustTabSizes()
        {
            using (Graphics g = this.CreateGraphics())
            {
                int maxWidth = 0;
                for (int i = 0; i < uiTabControl1.TabCount; i++)
                {
                    // 获取选项卡文本的大小
                    SizeF textSize = g.MeasureString(uiTabControl1.TabPages[i].Text, uiTabControl1.Font);
                    maxWidth = Math.Max(maxWidth, (int)textSize.Width);
                }

                // 设置每个选项卡的大小
                uiTabControl1.ItemSize = new Size(maxWidth + 20, uiTabControl1.ItemSize.Height);
            }
        }
        /// <summary>
        /// 自定义绘制每个选项卡的背景和文本，确保文本居中且为黑色。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
        {
            TabPage tabPage = uiTabControl1.TabPages[e.Index];
            Rectangle tabRect = uiTabControl1.GetTabRect(e.Index);

            // 绘制背景
            e.Graphics.FillRectangle(SystemBrushes.Control, tabRect);

            // 绘制文本
            TextRenderer.DrawText(e.Graphics, tabPage.Text, uiTabControl1.Font, tabRect, Color.Black, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
        }
    }
}
